package ph.loong.deliveryman.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.yarolegovich.slidingrootnav.SlidingRootNav;
import com.yarolegovich.slidingrootnav.SlidingRootNavBuilder;
import com.yarolegovich.slidingrootnav.callback.DragStateListener;

import java.util.ArrayList;

import deliveryman.loong.ph.deliveryman.R;
import ph.loong.deliveryman.fragment.main.UnQiangdanFragment;
import ph.loong.deliveryman.fragment.main.UnQuhuoFragment;
import ph.loong.deliveryman.fragment.main.UnSongdaFragment;
import ph.loong.deliveryman.ui.notify.NotifyActivity;
import ph.loong.deliveryman.ui.wallet.MyWalletActivity;
import ph.loong.deliveryman.ui.work.WorkActivity;
import ph.loong.deliveryman.utils.ViewFindUtils;
import ph.loong.deliveryman.view.StatusBarUtil;

public class MainActivity extends AppCompatActivity implements DragStateListener, View.OnClickListener {
    private SlidingRootNav slidingRootNav;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "待抢单", "待取货", "待送达"
    };
    private MyPagerAdapter mAdapter;
    private ViewPager mVp;
    private AppBarLayout mAppBarlayout;
    private SlidingTabLayout mBannerTab;
    private TextView mHandleCount;
    private TextView mMyWallet;
    private TextView mNotifyCenter;
    private TextView mAwardAction;
    private TextView mWordSum;
    private TextView mHelpCenter;
    private TextView mAppCenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StatusBarUtil.setColorNoTranslucent(this, this.getResources().getColor(R.color.colorAccent));
//        QMUIStatusBarHelper.translucent(this);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        mAppBarlayout = findViewById(R.id.app_bar_layout);
        mHandleCount = findViewById(R.id.handle_count);
        setSupportActionBar(toolbar);
        slidingRootNav = new SlidingRootNavBuilder(this)
                .withToolbarMenuToggle(toolbar)
                .withMenuOpened(false)
                .withRootViewScale(0.9f)
                .withContentClickableWhenMenuOpened(true)
                .withSavedState(savedInstanceState)
                .withMenuLayout(R.layout.menu_left_drawer)
                .addDragStateListener(this)
                .inject();

        mFragments.add(new UnQiangdanFragment());
        mFragments.add(new UnQuhuoFragment());
        mFragments.add(new UnSongdaFragment());
        View decorView = getWindow().getDecorView();
        mVp = ViewFindUtils.find(decorView, R.id.vp);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mVp.setAdapter(mAdapter);
        mBannerTab = ViewFindUtils.find(decorView, R.id.banner_tab);
        mBannerTab.setViewPager(mVp);
        findViewByDrawer();
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeTabBg(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //todo 模拟数据
        SpannableString spannableString = new SpannableString(3 + "个");
        AbsoluteSizeSpan span = new AbsoluteSizeSpan(60, true);
        spannableString.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mHandleCount.setText(spannableString);
    }

    private void findViewByDrawer() {
        mMyWallet = findViewById(R.id.my_wallet);
        mNotifyCenter = findViewById(R.id.notify_center);
        mAwardAction = findViewById(R.id.award_action);
        mWordSum = findViewById(R.id.work_sum);
        mHelpCenter = findViewById(R.id.help_center);
        mAppCenter = findViewById(R.id.app_center);

        mMyWallet.setOnClickListener(this);
        mNotifyCenter.setOnClickListener(this);
        mAwardAction.setOnClickListener(this);
        mWordSum.setOnClickListener(this);
        mHelpCenter.setOnClickListener(this);
        mAppCenter.setOnClickListener(this);

    }

    /**
     * 滑动viewpager时改变头部的背景颜色
     */
    private void changeTabBg(int position) {
        switch (position) {
            case 0:
                mAppBarlayout.setBackground(getResources().getDrawable(R.drawable.banner_red));
                break;
            case 1:
                mAppBarlayout.setBackground(getResources().getDrawable(R.drawable.banner_yellow));
                break;
            case 2:
                mAppBarlayout.setBackground(getResources().getDrawable(R.drawable.banner_bule));
                break;
        }

    }

    @Override
    public void onDragStart() {
        StatusBarUtil.setColorNoTranslucent(this, this.getResources().getColor(R.color.white));
    }

    @Override
    public void onDragEnd(boolean isMenuOpened) {
        if (isMenuOpened) {
            StatusBarUtil.setColorNoTranslucent(this, this.getResources().getColor(R.color.white));
        } else {
            StatusBarUtil.setColorNoTranslucent(this, this.getResources().getColor(R.color.colorAccent));
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.my_wallet:
                //我的钱包
                intent = new Intent(this, MyWalletActivity.class);
                break;

            case R.id.notify_center:
                //通知中心
                intent = new Intent(this, NotifyActivity.class);
                break;

            case R.id.award_action:
                //奖励活动
                intent = new Intent(this, MyWalletActivity.class);
                break;

            case R.id.work_sum:
                //工作汇总
                intent = new Intent(this, WorkActivity.class);
                break;

            case R.id.help_center:
                //帮助中心
                intent = new Intent(this, MyWalletActivity.class);
                break;

            case R.id.app_center:
                //应用设置
                intent = new Intent(this, MyWalletActivity.class);
                break;

        }
        startActivity(intent);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }


}
